package com.haobangs.imms.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haobangs.imms.mapper.DeviceMapper;
import com.haobangs.imms.smsUtils.AliyunSmsService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.haobangs.imms.domain.Device;
import com.haobangs.imms.service.IDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 设备管理Controller
 *
 * @author haobangs
 * @date 2023-06-12
 */
@Slf4j
@RestController
@RequestMapping("/imms/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private RedisCache redisCache;


    /**
     * 查询设备管理列表
     */
//    @PreAuthorize("@ss.hasPermi('imms:device:list')")
    @GetMapping("/page")
    public Page getByPage(Device device)
    {
       return deviceService.getByPage(device);
    }

    /**
     * 查询设备管理列表
     */
//    @PreAuthorize("@ss.hasPermi('imms:device:list')")
    @GetMapping("/list")
    public AjaxResult getList()
    {
        return AjaxResult.success(deviceMapper.selectList(null));
    }

    /**
     * 导出设备管理列表
     */
//    @PreAuthorize("@ss.hasPermi('imms:device:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('imms:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceService.selectDeviceById(id));
    }

    /**
     * 新增设备管理
     */
//    @PreAuthorize("@ss.hasPermi('imms:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备管理
     */
//    @PreAuthorize("@ss.hasPermi('imms:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Device device)
    {
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备管理
     */
//    @PreAuthorize("@ss.hasPermi('imms:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }

    /*
    * 从Redis中获取设备状态集合
    * */
    @GetMapping("/status/map/{workshopId}")
    public AjaxResult getDeviceStatusMap(@PathVariable("workshopId") Long workshopId){
        Map<String, String> statusMap = redisCache.getCacheMap(Constants.DEVICE_STATUS + workshopId);
        if (Objects.isNull(statusMap) || statusMap.size() == 0){
            statusMap = deviceService.selectDeviceStatus();
            redisCache.setCacheMap(Constants.DEVICE_STATUS + workshopId, statusMap);
            return AjaxResult.success(statusMap);
        }else{
            return AjaxResult.success(statusMap);
        }
    }
}
